package models;

import helpers.StringHelper;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import javax.cache.Cache;
import javax.cache.CacheException;
import javax.cache.CacheManager;

import com.googlecode.objectify.Key;
import com.googlecode.objectify.Objectify;
import com.googlecode.objectify.ObjectifyOpts;
import com.googlecode.objectify.ObjectifyService;
import com.googlecode.objectify.Query;

import entities.Category;

public class CategoryModel {
	
	private Objectify ofy;

	private Cache cache = null;

	private String prefix = "category_";

	private static CategoryModel instance = null;
	
	public static CategoryModel getInstance() {
		if (instance == null) {
			instance = new CategoryModel();
		}
		
		return instance;
	}
	
	private CategoryModel() {
		try {
			ObjectifyService.register(Category.class);
		} catch (Exception e) {
			// e.printStackTrace();
		}

		try {
			cache = CacheManager.getInstance().getCacheFactory()
					.createCache(Collections.emptyMap());
		} catch (CacheException e) {
			// e.printStackTrace();
		}

		ObjectifyOpts opts = new ObjectifyOpts().setSessionCache(true);
		ofy = ObjectifyService.begin(opts);
	}

	public List<Category> list(int limit, int page) {
		List<Category> categories = new ArrayList<Category>();
		
		String cacheName = prefix + "page_" + page + "_limit_" + limit;
		boolean isCached = false;
		
		if (cache != null) {
			try {
				categories = (List<Category>) cache.get(cacheName);
				if (categories != null) isCached = true;
			} catch (Exception e) {
			}
		}
		
		if (!isCached) {
			int start = (page - 1) * limit;
			categories = ofy.query(Category.class).limit(limit).offset(start).list();
			
			if (cache != null) {
				try {
					cache.put(cacheName, categories);
				} catch (Exception e) {
					// TODO: handle exception
				}
			}
		}

		return categories;
	}

	public List<Category> getAll() {
		List<Category> categories = new ArrayList<Category>();
		
		String cacheName = prefix + "list_all_categories";
		boolean isCached = false;
		
		if (cache != null) {
			try {
				categories = (List<Category>) cache.get(cacheName);
				if (categories != null) isCached = true;
			} catch (Exception e) {
				// TODO: handle exception
			}
		}
		
		if (!isCached) {
			categories = ofy.query(Category.class).list();
			if (cache != null) {
				try {
					cache.put(cacheName, categories);
				} catch (Exception e) {
				}
			}
		}
		
		return categories;
	}

	public void insert(Category category) {
		if (cache != null) {
			try {
				cache.remove(prefix + "list_all_categories");
			} catch (Exception e) {
				// TODO: handle exception
			}
		}
		
		ofy.put(category);
	}

	public void delete(Category category) {
		//remove cache all
		if (cache != null) {
			try {
				cache.remove(prefix + "list_all_categories");
			} catch (Exception e) {
				// TODO: handle exception
			}
		}

		ofy.delete(category);
	}

	public Category getCategory(String id) {
		Category category = new Category();
		try {
			category = ofy.get(new Key<Category>(Category.class, id));
		} catch (Exception e) {
			category = null;
		}
		
		return category;
	}

	public Category getCategoryByAlias(String alias) {
		Category category = new Category();
		try {
			category = ofy.query(Category.class).filter("alias", alias).get();
		} catch (Exception e) {
			category = null;
		}
		return category;
	}
	
	public Map<String, Category> getByListIds(List<String> ids) {
		Map<String, Category> categories = new TreeMap<String, Category>();
		
		Category category = null;
		
		for (String id : ids) {
			category = getCategory(id);
			
			if (category != null) {
				categories.put(id, category);
			}
		}
		
		return categories;
	}
}
